package com.coca1cole.redis.init;

import com.coca1cole.redis.util.SpringContextUtil;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class InitCache implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
        ApplicationContext applicationContext = SpringContextUtil.getApplicationContext();
        //获取子类集合
        Map<String,AbstractCache> beanMap = applicationContext.getBeansOfType(AbstractCache.class);
        if(beanMap.isEmpty()){
            return;
        }
        //遍历子类进行预热
        for (Map.Entry<String,AbstractCache> entry: beanMap.entrySet()) {
            AbstractCache abstractCache = (AbstractCache) SpringContextUtil.getBean(entry.getValue().getClass());
            abstractCache.initCache();
        }
    }
}
